<?php

namespace controller;

/**
 * Description of TerritorialUnits
 *
 * @author Damian
 */
class TerritorialUnits extends Controller {

    public function index() {
        $file = new \SplFileObject('spispna.csv');
        $delimiter = ';';
        $enclosure = "\n";
        $regions = array();
        $districts = array();
        $communities = array();
        $cities = array();
        $streets = array();
        $postCodes=array();
        $file->setCsvControl($delimiter, $enclosure);
        $data = $file->fgetcsv();
        $file->next();
        while ($file->valid()) {            
            $data = $file->fgetcsv();
            $street = (strlen($data[2])!=0)? $data[2] : "-1";
            if (!in_array($data[6], $regions)) {
                $regions[] = $data[6];
                $districts[$data[6]] = array();
                $districts[$data[6]][] = $data[5];
                $communities[$data[6]][$data[5]][] = $data[4];
                $cities[$data[6]][$data[5]][$data[4]][] = $data[1];
                $streets[$data[6]][$data[5]][$data[4]][$data[1]][] = $street;
                $postCodes[$data[6]][$data[5]][$data[4]][$data[1]][$street][]=$data[0];
            } 
            else if (!in_array($data[5], $districts[$data[6]])) {
                $districts[$data[6]][] = $data[5];
                $communities[$data[6]][$data[5]][] = $data[4];
                $cities[$data[6]][$data[5]][$data[4]] = array();
                $cities[$data[6]][$data[5]][$data[4]][] = $data[1];
                $streets[$data[6]][$data[5]][$data[4]][$data[1]][] = $street;
                $postCodes[$data[6]][$data[5]][$data[4]][$data[1]][$street][]=$data[0];
            } 
            else if (!in_array($data[4], $communities[$data[6]][$data[5]])) {
                $communities[$data[6]][$data[5]][] = $data[4];
                $cities[$data[6]][$data[5]][$data[4]] = array();
                $cities[$data[6]][$data[5]][$data[4]][] = $data[1];
                $streets[$data[6]][$data[5]][$data[4]][$data[1]][] = $street;
                $postCodes[$data[6]][$data[5]][$data[4]][$data[1]][$street][]=$data[0];
            } 
            else if (!in_array($data[1], $cities[$data[6]][$data[5]][$data[4]])) {
                $cities[$data[6]][$data[5]][$data[4]][] = $data[1];
                $streets[$data[6]][$data[5]][$data[4]][$data[1]][] = $street;
                $postCodes[$data[6]][$data[5]][$data[4]][$data[1]][$street][]=$data[0];
            } 
            else if (!in_array($street, $streets[$data[6]][$data[5]][$data[4]][$data[1]])) {
                $cities[$data[6]][$data[5]][$data[4]][] = $data[1];
                $streets[$data[6]][$data[5]][$data[4]][$data[1]][] = $street;
                $postCodes[$data[6]][$data[5]][$data[4]][$data[1]][$street][]=$data[0];
            }
            else if (!in_array($data[0], $postCodes[$data[6]][$data[5]][$data[4]][$data[1]][$street])) {
                $postCodes[$data[6]][$data[5]][$data[4]][$data[1]][$street][]=$data[0];
            }
            $file->next();
        }
        $regionID=0;
        $districtID=0;
        $communityID=0;
        $cityID=0;
        $streetID=1;
        $regionsCsv=new \SplFileObject("regions.csv","w");
        $districtsCsv = new \SplFileObject("districts.csv","w");
        $communitiesCsv = new \SplFileObject("communities.csv","w");
        $citiesCsv = new \SplFileObject("cities.csv","csv");
        $streetsCsv= new \SplFileObject("streets.csv","w");
        $codesCsv = new \SplFileObject("codes.csv","w");
        $streetsCsv->fwrite("brak\n");
        foreach ($postCodes as $region => $regionArray){
            $regionID++;
            $regionsCsv->fwrite($region."\n");
            foreach ($regionArray as $district =>$districtArray){
                $districtID++;
                $districtsCsv->fwrite($district."\n");
                foreach ($districtArray as $community => $communityArray) {
                    $communityID++;
                    $communitiesCsv->fwrite($community."\n");
                    foreach ($communityArray as $city => $cityArray) {
                        $cityID++;
                        $citiesCsv->fwrite($city."\n");
                        foreach ($cityArray as $street => $streetArray){
                            if ($street!="-1"){
                                $streetID++;
                                $streetsCsv->fwrite($street."\n");
                                foreach ($streetArray as $code){
                                    $codesCsv->fwrite($regionID.";".$districtID.";".$communityID.";".$cityID.";".$streetID.";".$code."\n");
                                }
                            }
                            else {
                                 foreach ($streetArray as $code){
                                    $codesCsv->fwrite($regionID.";".$districtID.";".$communityID.";".$cityID.";1;".$code."\n");
                                }
                            }
                            
                        }
                    }
                }
            }
        }
    }

}
